package com.example.universita.esami;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.MenuItem.OnMenuItemClickListener;
import android.content.DialogInterface;
import android.database.Cursor;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.content.Intent;


public class Note extends Activity {
	private final static int POS_NOTE=4;
	private boolean flag=false;
	private EditText et;
	private Button b; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        Intent intent = getIntent();
        String pkg = getPackageName();
        String str = intent.getStringExtra(pkg+".myString");
        if(str!=null){
        	restoreData(str);
        }
        
        Button salva = (Button) findViewById(R.id.salva_note);
        
        salva.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				invio();
			}
		});
    }
    
    public void selezionaEsame(View v){
    	
    	int i =0;
    	DBDataUniversity db = new DBDataUniversity(this);
    	db.open();
    	Cursor c=db.getExams();
    	
    	
    	
    	final String[] options= new String[c.getCount()];
    	
    	c.moveToFirst();
    	
    	while(c.moveToNext()){
			
			options[i]=c.getString(1);
			i++;
			
		}
    	
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(getString(R.string.an_seleziona_esame));
		builder.setItems(options, new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which) {
				String option = options[which];
				restoreData(option);
				
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
		db.close();
    }
    
    public void restoreData(String str){
    	
    	b = (Button) findViewById(R.id.buttonnote);
		et = (EditText) findViewById(R.id.note);
		DBDataUniversity db = new DBDataUniversity(getBaseContext());
    	db.open();
    	b.setText(str);
		et.setText(db.getNote(str));
		flag=true;
		db.close();
    	
    }
     
    public void invio(){
    	
    	Toast t;
    	
    	EditText et = (EditText) findViewById(R.id.note);
    	
    	if(et.getText().toString().equals("No") || et.getText().toString().equals("") )
    	{
    		Toast.makeText(this, getString(R.string.an_error), Toast.LENGTH_SHORT).show();
    	}
    	else{
	    	
	    	if(flag==true){
	    		
	    		b = (Button) findViewById(R.id.buttonnote);
	        	String esame = b.getText().toString();
	        	
	    		String note = et.getText().toString();
	        	t = Toast.makeText(this, getString(R.string.an_ok), Toast.LENGTH_SHORT);
	        	
	        	DBDataUniversity db = new DBDataUniversity(this);
	        	db.open();
	        	db.insertNote(esame, note);
	        	t.show();
	        	b.setText(getString(R.string.an_seleziona_esame));
	        	et.setText(getString(R.string.an_ins));
	        	flag=false;
	        	db.close();
	        	
	    	}else{	
	    	
				t = Toast.makeText(this, getString(R.string.an_error), Toast.LENGTH_SHORT);
				t.show();
			
	    	}
    	}
    	
    	
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_note, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	Intent intent;
    	String pkg;
    	Cursor cursor;
    	ListView listView;
    	
    	switch(item.getItemId()){
    	    case R.id.statistiche: // Vai a Dettagli Esame
 	    	   		intent = new Intent (this, StatisticActivity.class);
 	    	   		startActivity(intent);
 	    	   		break;  
    	    case R.id.reset: // Resetta EditText
	    	   		ResetValues();
	    	   		break; 
    	    case R.id.dettagli: // Vai a Dettagli Esame
	    	   		intent = new Intent (this, Details.class);
	    	   		startActivity(intent);
	    	   		break;       		
    	    case R.id.carriera: // Vai a Carriera
 	    	   		intent = new Intent(this, Carriera.class);
 	    	   		startActivity(intent);  
 	    	   		break;             
    	    case R.id.home: // Vai alla Home
   	   	   			intent = new Intent(this, MainActivity.class);
   	   	   			startActivity(intent);  
   	   	   			break;  
    	    case R.id.inserisci: // Vai a Dettagli Esame
	    	   		invio();
	    	   		break; 
    	    case R.id.annulla: // Vai a Dettagli Esame
    	   			finish();
    	   			break;    		
    	}
     return true;
    }
    
    
    public void ResetValues(){
    	b = (Button) findViewById(R.id.buttonnote);
    	et = (EditText) findViewById(R.id.note);
    	DBDataUniversity db = new DBDataUniversity(getBaseContext());
    	
    	if(flag==true){
    		
        	db.open();
	    	Cursor c = db.getExamsDetails(b.getText().toString());
	    	c.moveToNext();
	    	et.setText(c.getString(POS_NOTE));
	    	db.close();
	    	
    	}	
    	
    }
    
}